<?php
/**
 * CreateTime: 2022/6/28 16:30
 */

namespace App\Http\Controllers;

use App\Consts\ClientConsts\ClientConsts;
use App\Consts\LendingConsts\LendingConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\TableConsts\TableConsts;
use App\Enum\EnterpriseEnum\EnterpriseStatusEnum;
use App\Func\ResponseFunc;
use App\Http\Requests\File\ExportExcelRequest;
use App\Models\Client\ClientModel;
use App\Tools\File\ExcelFile;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Storage;
use JetBrains\PhpStorm\NoReturn;


class ExportController extends Controller
{
    private function fmtHead($head){
        return array_column($head,'fieldName','field');
    }
    private function fmtIds($data){
        return explode(",",$data);
    }
    private function fmtSelect($data){
        $data = $this->fmtHead($data);
        $newData = [];
        foreach($data as $key => $value){
            $newData[] = $key." as ".$value;
        }
        return $newData;
    }

    public function exportClient(ExportExcelRequest $request){
        $data = $request->all();
        $head = $data['exHead'];
        $fileName = $data['exName'] ?? "";
        $fmtHead = $this->fmtHead($head);
        $select = $this->fmtSelect($head);
        $ids = $this->fmtIds($data['exIds']);
        $newClient = new ClientModel();
        $list = $newClient->getClientInfoByClientIds($ids,$select);
        // 处理list数据
        $ef = new ExcelFile();
        $ef->fileExport($fmtHead,$list->toArray(),$fileName);
    }

    public function exportClientV2()
    {
        $data = request()->all();
        $ef = new ExcelFile();
        $head = [
            "ID",
            "公司名称",
            "相对方关系",
            "所在区域",
            "企业类型",
            "企业性质",
            "风险等级",
            "客户类型",
            "公司状态",
            "更新时间"
        ];
        $ids = $data['exIds'] ?? [];
        $fileName = $data['exName'] ?? "";
        $clientModel = new ClientModel();
        $data = $clientModel->getClientListByClientIds($ids,[
            'client_id',
            'client_name',
            'client_type',
            'client_counterparty_type',
            'client_company_status',
            'client_territory_code',
            'client_industry_type',
            'client_economic_type',
            'client_risk_rank',
            'client_status',
            'updated_at',
        ]);
        // 处理数据
        if($data){
            $data = $data->toArray();
            $newData = [];
            foreach($data as $value){
                $newData[] = [
                  'client_id' => $value['client_id'],
                  'client_name' => $value['client_name'],
                  'client_counterparty_type_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::CLIENT_COUNTERPARTY, $value['client_counterparty_type']),
                  'client_territory_code_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::CLIENT_TERRITORY_CODE, $value['client_territory_code']),
                  'client_industry_type_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::ENTERPRISE_TYPE, $value['client_industry_type']),
                  'client_economic_type_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::ECONOMIC_TYPE, $value['client_economic_type']),
                  'client_risk_rank_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::CLIENT_RISK_RANK_KEY, $value['client_risk_rank']),
                  'client_type_name' => app()->gcHelper->getDictionaryMapValueByParentKey(ClientConsts::XYKH_CLIENT_TYPE, $value['client_type']),
                  'client_company_status_name' => EnterpriseStatusEnum::getEnumMeaningByEnumCode($value['client_company_status']),
                  'updated_at' => $value['updated_at'],
                ];
            }
        }else{
            return ResponseFunc::returnJson(
                'error',
                null,
                '导出失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            );
        }

        return ResponseFunc::returnJson(
            'ok',
            [
                "filename" => $ef->fileAllExport($head,$newData,TableConsts::CY_LOGIN_LOG_TABLE_NAME,$fileName)
            ],
            '文件渲染成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
//        return $ef->fileAllExport($head,$data,TableConsts::CY_LOGIN_LOG_TABLE_NAME,$fileName);
    }

    public function exportDownload(){
        $data = request()->all();
        if(!is_file(Storage::disk('local')->path($data['filename']))){
            return ResponseFunc::returnJson(
                'error',
                null,
                '导出失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            );
        }
        return response()->download(Storage::disk('local')->path($data['filename']))->deleteFileAfterSend();
    }
}